home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 10253 < prev    next >
Encoding:
Text File  |  1996-08-05  |  6.7 KB  |  323 lines

  1. Path: news.voicenet.com!usenet
  2. From: deaton@cygnus.rsabbs.com
  3. Newsgroups: comp.lang.c
  4. Subject: Bizarre error with structures, please help!
  5. Date: 16 Mar 1996 05:44:24 GMT
  6. Organization: Voicenet - Internet Access - (215)674-9290
  7. Message-ID: <4idkfo$1el@news.voicenet.com>
  8. NNTP-Posting-Host: d02.rsabbs.com
  9. X-Newsreader: SPRY News 3.03 (SPRY, Inc.)
  10.  
  11.  
  12.    I'm writing a short program to init the default values for a data file. All my routine does is assign hardcoded (ack!) values to the
  13. variables the structure array, and then save it to a random access file via fwrite(). And just to test the code, I open the file with an fread() 
  14. and print the values to the screen with a "for" loop. EVERYTHING looks fine, but in the 13th element, when the variable wtype is set to 
  15. 13 (same value as array reference) the values from that structure on go crazy (except the empty ones at the end). I'm going nuts, I've already
  16. spent over ten hours trying to find the bug in this simple thing... ANY help is REALLY appreciated! :) Thanks in advance!
  17.  
  18.   Here's the code:
  19.  
  20. #define WEP_REF 30    //Max number of weapon slots in weapon ref array.
  21.  
  22. typedef struct 
  23. {
  24.    char type[20];  //Weapon name.
  25.    int  cost;        //Cost of weapon in Gold.
  26.    int  damhi;     //High range of damage for the weapon.
  27.    int  damlo;     //Low range of damage for the weapon.
  28.    int  wtype;     //Numerical weapon reference.
  29.    int  missle;     //Is the weapon a missle weapon? (1 or 0)
  30.    char size;      //'S'mall or 'L'arge. No shield with large.
  31.    int  actual;    //Actual damage done directly to a unit.
  32.    int  special;   //Special case damage. If 1, actual damage ammount is dealt
  33.                        //to the armor, and remainder is done to the unit.
  34. }weaponS;
  35.  
  36. //****** PROGRAM TO MAKE DEFAULT WEAPON FILE *********
  37.  
  38. #include "stdio.h"
  39. #include "conio.h"
  40. #include "ctype.h"
  41. #include "D:\doug's\orcs\struct.h"
  42.  
  43. static weaponS w[WEP_REF];
  44.  
  45. main()
  46. {//MAIN
  47.  
  48.    register int count;
  49.    FILE * fptr;
  50.    printf("\n\n\n\n\n\n\Populating Structure.");
  51.  
  52.    strcpy(w[0].type,"Dagger");
  53.    w[0].cost=0;
  54.    w[0].size='s';
  55.    w[0].wtype=0;
  56.    w[0].damhi=4;
  57.    w[0].damlo=1;
  58.    w[0].actual=1;
  59.    w[0].special=0;
  60.    w[0].missle=0;
  61.  
  62.    putch('.');
  63.  
  64.    strcpy(w[1].type,"Sabre");
  65.    w[1].cost=0;
  66.    w[1].size='s';
  67.    w[1].wtype=1;
  68.    w[1].damhi=5;
  69.    w[1].damlo=2;
  70.    w[1].actual=1;
  71.    w[1].special=0;
  72.    w[1].missle=0;
  73.  
  74.    putch('.');
  75.  
  76.    strcpy(w[2].type,"Short Sword");
  77.    w[2].cost=0;
  78.    w[2].size='s';
  79.    w[2].wtype=2;
  80.    w[2].damhi=6;
  81.    w[2].damlo=1;
  82.    w[2].actual=1;
  83.    w[2].special=0;
  84.    w[2].missle=0;
  85.  
  86.    putch('.');
  87.  
  88.    strcpy(w[3].type,"Club");
  89.    w[3].cost=0;
  90.    w[3].size='s';
  91.    w[3].wtype=3;
  92.    w[3].damhi=6;
  93.    w[3].damlo=1;
  94.    w[3].actual=1;
  95.    w[3].special=0;
  96.    w[3].missle=0;
  97.  
  98.    putch('.');
  99.  
  100.    strcpy(w[4].type,"Hammer");
  101.    w[4].cost=0;
  102.    w[4].size='s';
  103.    w[4].wtype=4;
  104.    w[4].damhi=6;
  105.    w[4].damlo=1;
  106.    w[4].actual=1;
  107.    w[4].special=0;
  108.    w[4].missle=0;
  109.  
  110.    putch('.');
  111.  
  112.    strcpy(w[5].type,"Long Knife");
  113.    w[5].cost=0;
  114.    w[5].size='s';
  115.    w[5].wtype=5;
  116.    w[5].damhi=6;
  117.    w[5].damlo=1;
  118.    w[5].actual=1;
  119.    w[5].special=0;
  120.    w[5].missle=0;
  121.  
  122.    putch('.');
  123.  
  124.    strcpy(w[6].type,"Broadsword");
  125.    w[6].cost=0;
  126.    w[6].size='s';
  127.    w[6].wtype=6;
  128.    w[6].damhi=8;
  129.    w[6].damlo=1;
  130.    w[6].actual=1;
  131.    w[6].special=0;
  132.    w[6].missle=0;
  133.  
  134.    putch('.');
  135.  
  136.    strcpy(w[7].type,"Scimitar");
  137.    w[7].cost=1;
  138.    w[7].size='s';
  139.    w[7].wtype=7;
  140.    w[7].damhi=8;
  141.    w[7].damlo=1;
  142.    w[7].actual=1;
  143.    w[7].special=0;
  144.    w[7].missle=0;
  145.  
  146.    putch('.');
  147.  
  148.    strcpy(w[8].type,"Mace");
  149.    w[8].cost=0;
  150.    w[8].size='s';
  151.    w[8].wtype=8;
  152.    w[8].damhi=8;
  153.    w[8].damlo=1;
  154.    w[8].actual=1;
  155.    w[8].special=0;
  156.    w[8].missle=0;
  157.  
  158.    putch('.');
  159.  
  160.    strcpy(w[9].type,"Long Sword");
  161.    w[9].cost=0;
  162.    w[9].size='s';
  163.    w[9].wtype=9;
  164.    w[9].damhi=10;
  165.    w[9].damlo=1;
  166.    w[9].actual=1;
  167.    w[9].special=0;
  168.    w[9].missle=0;
  169.  
  170.    putch('.');
  171.  
  172.    strcpy(w[10].type,"Morning Star");
  173.    w[10].cost=0;
  174.    w[10].size='s';
  175.    w[10].wtype=10;
  176.    w[10].damhi=8;
  177.    w[10].damlo=2;
  178.    w[10].actual=2;
  179.    w[10].special=0;
  180.    w[10].missle=0;
  181.  
  182.    putch('.');
  183.  
  184.    strcpy(w[11].type,"Battle Axe");
  185.    w[11].cost=0;
  186.    w[11].size='l';
  187.    w[11].wtype=11;
  188.    w[11].damhi=8;
  189.    w[11].damlo=2;
  190.    w[11].actual=2;
  191.    w[11].special=0;
  192.    w[11].missle=0;
  193.  
  194.    putch('.');
  195.  
  196.    strcpy(w[12].type,"Spear");
  197.    w[12].cost=1;
  198.    w[12].size='l';
  199.    w[12].wtype=12;
  200.    w[12].damhi=12;
  201.    w[12].damlo=2;
  202.    w[12].actual=2;
  203.    w[12].special=0;
  204.    w[12].missle=0;
  205.  
  206.    putch('.');
  207.  
  208.    strcpy(w[13].type,"Warhammer");
  209.    w[13].cost=0;
  210.    w[13].size='l';
  211.    w[13].wtype=13;  //<-----THIS IS THE PROBLEM LINE--WHEN IT'S SET TO 13,
  212.    w[13].damhi=12;  //THE WHOLE THING GOES HAYWIRE!!
  213.    w[13].damlo=2;
  214.    w[13].actual=2;
  215.    w[13].special=0;
  216.    w[13].missle=0;
  217.  
  218.    putch('.');
  219.  
  220.    strcpy(w[14].type,"Boar Spear");
  221.    w[14].cost=2;
  222.    w[14].size='l';
  223.    w[14].wtype=14;
  224.    w[14].damhi=12;
  225.    w[14].damlo=3;
  226.    w[14].actual=3;
  227.    w[14].special=0;
  228.    w[14].missle=0;
  229.  
  230.    putch('.');
  231.  
  232.    strcpy(w[15].type,"Halberd");
  233.    w[15].cost=0;
  234.    w[15].size='l';
  235.    w[15].wtype=15;
  236.    w[15].damhi=12;
  237.    w[15].damlo=3;
  238.    w[15].actual=3;
  239.    w[15].special=0;
  240.    w[15].missle=0;
  241.  
  242.    putch('.');
  243.  
  244.    strcpy(w[16].type,"Bow");
  245.    w[16].cost=0;
  246.    w[16].size='l';
  247.    w[16].wtype=16;
  248.    w[16].damhi=6;
  249.    w[16].damlo=1;
  250.    w[16].actual=1;
  251.    w[16].special=0;
  252.    w[16].missle=1;
  253.  
  254.    putch('.');
  255.  
  256.    strcpy(w[17].type,"Crossbow");
  257.    w[17].cost=3;
  258.    w[17].size='l';
  259.    w[17].wtype=17;
  260.    w[17].damhi=8;
  261.    w[17].damlo=2;
  262.    w[17].actual=2;
  263.    w[17].special=1;
  264.    w[17].missle=1;
  265.  
  266.    putch('.');
  267.  
  268.    printf("\nOpening file for writing...");
  269.    fptr=fopen("weapon.dat","w+"); //Open file for writing. If file
  270.                      //exists, erase and create a new one.
  271.    printf("\nWriting Structure to Disk...");
  272.    fwrite(w,sizeof(w),1,fptr);
  273.  
  274.    printf("\nClosing file...");
  275.    fclose(fptr);
  276.  
  277.    printf("\nSuccess!");
  278.  
  279.    return(0);
  280.  
  281. }//MAIN
  282.  
  283. ==================================TEST PROGRAM
  284. #include "stdio.h"
  285. #include "conio.h"
  286. #include "d:\doug's\orcs\struct.h"
  287.  
  288. main()
  289. {
  290.    register int count;
  291.    FILE * fptr;
  292.    static weaponS w[WEP_REF];
  293.    int junk;
  294.  
  295.    fptr=fopen("weapon.dat","r+");
  296.    fread(w,sizeof(w),1,fptr);
  297.    fclose(fptr);
  298.  
  299. //====================TEST-PRINT THE RESULTS=========================
  300. clrscr();
  301.  
  302. puts("Printing out default Weapon specifications...");
  303. for(count=0;count<WEP_REF;count++)
  304.   {
  305.     printf("\n\nName:%s",w[count].type);
  306.     printf("\nCost:%d",w[count].cost);
  307.     printf("\nwtype:%d",w[count].wtype);
  308.     printf("\nDamhi:%d",w[count].damhi);
  309.     printf("\nDamlo:%d",w[count].damlo);
  310.     printf("\nActual:%d",w[count].actual);
  311.     printf("\nSpecial:%d",w[count].special);
  312.     printf("\nMissle:%d",w[count].missle);
  313.     printf("\nSize:%c\n\n",w[count].size);
  314.     junk=getch();
  315.   }
  316.  
  317.    return(0);
  318. }//main()
  319.  
  320.  
  321.  
  322.  
  323.